package com.zl.controller;

import com.zl.domain.Query;
import com.zl.domain.Role;
import com.zl.service.RoleServcie;
import net.sf.json.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class RoleController {
    @Autowired
    private RoleServcie roleServcie;

    @RequestMapping(value = "/role/roleList", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject roleList(HttpServletRequest request) {
        return new JSONObject().accumulate("roles", roleServcie.findAll(((Map<String, Object>) request.getSession().getAttribute("user")).get("weight") + ""));
    }

    @RequestMapping(value = "/role/roles", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject projectList(@Param("Query") Query query, HttpServletRequest request) {
        query.setPrivilegeWeight(((Map<String, Object>) request.getSession().getAttribute("user")).get("weight") + "");
        return new JSONObject().accumulate("page", roleServcie.page(query));
    }

    @RequestMapping(value = "/role/delRole", method = RequestMethod.GET)
    public
    @ResponseBody
    JSONObject delRole(Role role) {
        return new JSONObject().accumulate("message", roleServcie.delete(role) == 0 ? "删除失败！！" : "删除成功！");
    }

    @RequestMapping(value = "/role/editRole", method = RequestMethod.POST)
    public
    @ResponseBody
    JSONObject editRole(Role role) {
        String message = "";
        if (role.getRid() == 0)
            message = roleServcie.insert(role) == 0 ? "添加失败！！" : "添加成功！";
        else
            message = roleServcie.update(role) == 0 ? "编辑失败！！" : "编辑成功！";
        return new JSONObject().accumulate("message", message);
    }
}
